package com.yh.aop;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKDynamicProxy implements InvocationHandler {

    Object target;

    public Object getProxy(Object target) {
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("======JDKDynamicProxy 调用之前=======");
		System.out.println(proxy.getClass().getName());
		byte[]  b= ProxyGenerator.generateProxyClass(proxy.getClass().getSimpleName(),proxy.getClass().getInterfaces());
		FileOutputStream out = new FileOutputStream("D:\\"+proxy.getClass().getSimpleName()+".class");
		out.write(b);
		out.flush();
		out.close();
        method.invoke(this.target, args);
        System.out.println("======JDKDynamicProxy 调用之后=======");
        return null;
    }


}
